package com.yunquan.ohana.ui.bindDevice

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityBindDeviceBinding
import com.yunquan.ohana.ui.scan.ScanActivity

class BindDeviceActivity : BaseActivity<ActivityBindDeviceBinding, BindDeviceViewModel>() {

    private val mActivityResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == ScanActivity.RESULT_CODE) {
            // 扫描二维码返回
            val bundle = result.data?.extras
            val text = bundle?.getString("text")
            text?.let {
                viewModel!!.deviceLogin(it)
            }
        }
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_bind_device
    }

    override fun initVariableId(): Int {
        return BR.bindDeviceViewModel
    }

    override fun initParam() {
        super.initParam()
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.white)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.btnScan.setOnClickListener {
            val intent = Intent(this, ScanActivity::class.java)
            mActivityResult.launch(intent)
        }
    }
}